﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace CorpStoreServer
{
    class CheckCommonCatalog
    {
        private Log log;

        /// <summary>
        /// This function checks to see if the common catalog has changed since it was
        /// last checked.  The idea here is that corpStoreServer could be run with the
        /// 'checkCommonCatalog' command line parameter from some sort of background
        /// task on a periodic basis.  Then when the common catalog was changed, the
        /// master catalog would be rebuilt based upon the latest version of the 
        /// common catalog.
        /// </summary>
        /// <param name="parms">No parms needed</param>
        public void Run(Log log, NameValueCollection parms, AppSettings appSettings)
        {
            string latestChecksum;

            try
            {
                this.log = log;
                if (commonCatalogHasChanged(appSettings, out latestChecksum))
                {
                    // common catalog changed - rebuild master catalog
                    logInfo("Common catalog has changed - attempting to rebuild master catalog");
                    UpdateMasterCatalog obj = new UpdateMasterCatalog();
                    obj.Run(log, new NameValueCollection(), appSettings);

                    // now update our checksum setting 
                    appSettings.LastCatalogChecksum = latestChecksum;
                    appSettings.Save();
                }
                else
                    logInfo("Common catalog has not changed - not rebuilding master catalog");
            }
            catch (Exception ex)
            {
                logErr("Unhandled exception in checkMasterCatalog: {0}", ex.Message);
            }
        } // main


        private bool commonCatalogHasChanged(AppSettings appSettings, out string latestChecksum)
        {
            bool blnChanged = false;
            string lastChecksum;
            string newChecksum;
            string errStr;

            latestChecksum = "";
            try
            {
                // first read value of last common catalog checksum
                string commonCatalogFn = appSettings.CommonCatalogFn;
                lastChecksum = appSettings.LastCatalogChecksum;

                // now calculate checksum of most recent common catalog
                newChecksum = GenLib.GetFileHash(commonCatalogFn, out errStr);

                if (newChecksum.Equals(lastChecksum))
                {
                    logInfo("common catalog at '{0}' has not changed!", commonCatalogFn);
                    latestChecksum = lastChecksum;
                }
                else
                {
                    logInfo("common catalog at '{0}' HAS changed", commonCatalogFn);

                    latestChecksum = newChecksum;
                    blnChanged = true;
                }
            }
            catch (Exception ex)
            {
                logErr("Unhandled exception in commonCatalogHasChanged: {0}", ex.Message);
            }

            return blnChanged;
        }



        private void logInfo(string msg, params object[] parms)
        {
            if (log != null)
                log.LogInfo(msg, parms);
        }


        private void logErr(string msg, params object[] parms)
        {
            if (log != null)
                log.LogErr(msg, parms);
        }

    }
}
